#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <string.h> 
#include <linux/input.h>

int main(int argc, char *argv[])
{
	int fd, ret;
	struct input_event ev;

	if(argc < 2) {
		printf("Usage:\r\n\t./keyinputApp /dev/input/eventX @ Open Key\r\n");
		return -1;
	}

	fd = open(argv[1], O_RDWR);
	if(0 > fd) {
		printf("Error: file %s open failed!\r\n", argv[1]);
		return -1;
	}
	for ( ; ; ) {
		ret = read(fd, &ev, sizeof(struct input_event));
		if (ret) {
			switch (ev.type) {
			/* 按键事件 */
			case EV_KEY:
				printf("key %d, state %d\r\n", ev.code, ev.value);
				break;
			/* 其他类型的事件，自行处理 */
			case EV_REL:
				break;
			case EV_ABS:
				break;
			case EV_MSC:
				break;
			case EV_SW:
				break;
			};
		}
		else {
			printf("Error: file %s read failed!\r\n", argv[1]);
			goto out;
		}
	}
out:
	/* 关闭设备 */
	close(fd);
	return 0;
}
